package Servlet;

import Entity.Player;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
 * @author ori
 */
public class PlayerServlet extends HttpServlet {

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setAttribute("playerProfiles", getPlayer(request.getParameter("username")));

        // Now that the controller has gathered the data from the model,
        // pass the data onto the view to render it as JSON.
        RequestDispatcher view = request.getRequestDispatcher("/profile_view.jsp");
        view.forward(request, response);
    }

    //get player information
    private List<Map<String, String>> getPlayer(String PlayerName) {
        List<Map<String, String>> profileInfo = new ArrayList<Map<String, String>>();

        try {

            Context ctx = (Context) new InitialContext().lookup("java:comp/env");
            EntityManager em = (EntityManager) ctx.lookup("persistence/LogicalName");

            //make query to find player by name.
            Query q = em.createNamedQuery("Player.findByPlayName").setParameter("playName", PlayerName);
            List<Player> results = q.getResultList();
            //put results into list of map of string.
            for (Player p : results) {
                Map<String, String> Players = new HashMap<String, String>();
                Players.put("userName", p.getPlayName());
                Players.put("furthestPlayedLevel", "" + p.getFurthestPlayedLevel());
                Players.put("accumScore", "" + p.getAccumScore());
                Players.put("picture", "" + p.getPicture());
                Players.put("isOnline", "" + p.getIsOnline());
                Players.put("email", "" + p.getEmail());
                Players.put("passWord", "" + p.getPassWord());
                profileInfo.add(Players);
            }
        } catch (Exception ex) {
            Logger.getLogger(PlayerServlet.class.getName()).log(Level.SEVERE, null, ex);
        }

        System.out.println("Player MapSize: " + profileInfo.size());
        return profileInfo;
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
